from abc import ABC, abstractmethod
from typing import List, Dict

class Enemy(ABC):
    def __init__(self, level: int):
        self.level = level
        self.abilities = self.get_abilities()
        self.max_hp = self.get_base_hp() * (1 + (level - 1) * 0.2)
        self.current_hp = self.max_hp
        self.next_action = None
        self.name = self.get_name()
    
    @abstractmethod
    def get_base_hp(self) -> int:
        pass
    
    @abstractmethod
    def get_abilities(self) -> List[Dict]:
        pass
    
    @abstractmethod
    def get_name(self) -> str:
        pass
    
    def take_damage(self, amount: int) -> None:
        """受到伤害"""
        self.current_hp = max(0, self.current_hp - amount)
    
    def take_action(self) -> Dict:
        """执行行动"""
        import random
        
        # 获取所有技能
        abilities = self.get_abilities()
        
        # 根据概率权重选择技能
        total_prob = sum(ability["probability"] for ability in abilities)
        rand_val = random.random() * total_prob
        
        current_prob = 0
        selected_ability = abilities[0]  # 默认选第一个
        
        for ability in abilities:
            current_prob += ability["probability"]
            if rand_val <= current_prob:
                selected_ability = ability
                break
        
        # 保存并返回选择的行动
        self.next_action = {
            "type": selected_ability["type"],
            "name": selected_ability["name"],
            "damage": selected_ability["damage"]
        }
        
        return self.next_action 